class PayeeMapping < ActiveRecord::Base

  has_many :entries
  belongs_to :category
  belongs_to :payee

  composed_of :amount_lower, :class_name => 'Money', :mapping => %w(amount_lower cents)
  composed_of :amount_upper, :class_name => 'Money', :mapping => %w(amount_upper cents)

  # Returns a string explaining the current mapping.
  def explain
    e = ["Selects payee '#{payee.name}'"]
    e << "and places in category #{category.full_name}" if category
    e << "with memo '#{memo}'" if memo
    e << (single_amount? ? "when the amount is #{amount}" : "when the amount is between #{amount_lower} and #{amount_upper}")
    e << "and the payee matches '#{match}'" unless match.empty?
    e.join(", ")
  end

  def single_amount?
    (amount_lower && amount_upper) && (amount_lower == amount_upper)
  end

  def amount=(amount)
    amount_lower = amount
    amount_upper = amount
  end

  def amount
    amount_lower
  end

end
